package com.cabal.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cabal.auth.dao.CabalAuthMapper;
import com.cabal.auth.dto.CabalToolItemScriptDTO;
import com.cabal.auth.dto.CabalToolItemSuitDTO;
import com.cabal.common.constants.SystemConstants;
import com.cabal.common.util.SnowflakeIdUtil;
import com.cabal.config.DynamicDataSourceContextHolder;
import com.cabal.mall.dao.CabalToolItemScriptMapper;
import com.cabal.mall.dao.CabalToolItemSuitMapper;
import com.cabal.mall.entity.CabalToolItemScript;
import com.cabal.mall.entity.CabalToolItemSuit;
import com.cabal.mall.service.CabalToolItemSuitService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class CabalToolItemSuitServiceImpl implements CabalToolItemSuitService {

    @Resource
    private CabalToolItemSuitMapper cabalToolItemSuitMapper;

    @Resource
    private CabalToolItemScriptMapper cabalToolItemScriptMapper;

    @Resource
    private CabalAuthMapper cabalAuthMapper;

    @Override
    public String insertCabalToolItemSuit(CabalToolItemSuitDTO cabalToolItemSuitDTO) {
        CabalToolItemSuit cabalToolItemSuit = new CabalToolItemSuit();
        cabalToolItemSuit.setId(SnowflakeIdUtil.getInstance().nextId());
        cabalToolItemSuit.setItemSuitName(cabalToolItemSuitDTO.getItemSuitName());
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        cabalToolItemSuitMapper.insert(cabalToolItemSuit);
        DynamicDataSourceContextHolder.removeContextKey();
        return "SUCCESS";
    }

    @Override
    public String updateCabalToolItemSuit(CabalToolItemSuitDTO cabalToolItemSuitDTO) {
        CabalToolItemSuit updateCabalToolItemSuit = new CabalToolItemSuit();
        updateCabalToolItemSuit.setId(Long.parseLong(cabalToolItemSuitDTO.getId()));
        updateCabalToolItemSuit.setItemSuitName(cabalToolItemSuitDTO.getItemSuitName());
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        cabalToolItemSuitMapper.updateById(updateCabalToolItemSuit);
        DynamicDataSourceContextHolder.removeContextKey();
        return "SUCCESS";
    }

    @Override
    public String insertCabalToolItemScript(CabalToolItemScriptDTO cabalToolItemScriptDTO) {
        CabalToolItemScript cabalToolItemScript = new CabalToolItemScript();
        BeanUtils.copyProperties(cabalToolItemScriptDTO, cabalToolItemScript);
        cabalToolItemScript.setId(SnowflakeIdUtil.getInstance().nextId());
        cabalToolItemScript.setSuitId(Long.parseLong(cabalToolItemScriptDTO.getParentId()));
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        cabalToolItemScriptMapper.insert(cabalToolItemScript);
        DynamicDataSourceContextHolder.removeContextKey();
        return "SUCCESS";
    }

    @Override
    public String updateCabalToolItemScript(CabalToolItemScriptDTO cabalToolItemScriptDTO) {
        CabalToolItemScript updateCabalToolItemScript = new CabalToolItemScript();
        BeanUtils.copyProperties(cabalToolItemScriptDTO, updateCabalToolItemScript);
        updateCabalToolItemScript.setId(Long.parseLong(cabalToolItemScriptDTO.getId()));
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        cabalToolItemScriptMapper.updateById(updateCabalToolItemScript);
        DynamicDataSourceContextHolder.removeContextKey();
        return "SUCCESS";
    }

    @Override
    public List<CabalToolItemSuitDTO> selectCabalToolItemSuitList() {
        List<CabalToolItemSuitDTO> cabalToolItemSuitDTOList = new ArrayList<>();
        QueryWrapper<CabalToolItemSuit> queryWrapper = new QueryWrapper<CabalToolItemSuit>();
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        List<CabalToolItemSuit> cabalToolItemSuitList =  cabalToolItemSuitMapper.selectList(queryWrapper);
        DynamicDataSourceContextHolder.removeContextKey();
        if(CollectionUtils.isEmpty(cabalToolItemSuitList)) {
            return cabalToolItemSuitDTOList;
        }

        for(CabalToolItemSuit cabalToolItemSuit : cabalToolItemSuitList) {
            CabalToolItemSuitDTO cabalToolItemSuitDTO = new CabalToolItemSuitDTO();
            BeanUtils.copyProperties(cabalToolItemSuit, cabalToolItemSuitDTO);
            cabalToolItemSuitDTO.setId(String.valueOf(cabalToolItemSuit.getId()));
            cabalToolItemSuitDTOList.add(cabalToolItemSuitDTO);
        }
        return cabalToolItemSuitDTOList;
    }

    @Override
    public List<CabalToolItemScriptDTO> selectCabalToolItemScriptList(String suitName) {
        List<CabalToolItemScriptDTO> cabalToolItemScriptDTOList = new ArrayList<>();

        QueryWrapper<CabalToolItemSuit> querySuitWrapper = new QueryWrapper<CabalToolItemSuit>();
        querySuitWrapper.lambda().eq(CabalToolItemSuit::getItemSuitName, suitName);
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        CabalToolItemSuit dbCabalToolItemSuit =  cabalToolItemSuitMapper.selectOne(querySuitWrapper);
        if(dbCabalToolItemSuit == null) {
            return cabalToolItemScriptDTOList;
        }

        List<CabalToolItemScript> cabalToolItemScriptList = this.selectCabalToolItemScriptListBySuitId(dbCabalToolItemSuit.getId());
        if(CollectionUtils.isEmpty(cabalToolItemScriptList)) {
            return cabalToolItemScriptDTOList;
        }

        for(CabalToolItemScript cabalToolItemScript : cabalToolItemScriptList) {
            CabalToolItemScriptDTO cabalToolItemScriptDTO = new CabalToolItemScriptDTO();
            BeanUtils.copyProperties(cabalToolItemScript, cabalToolItemScriptDTO);
            cabalToolItemScriptDTO.setId(String.valueOf(cabalToolItemScript.getId()));
            cabalToolItemScriptDTOList.add(cabalToolItemScriptDTO);
        }

        return cabalToolItemScriptDTOList;
    }

    @Override
    public String deleteCabalToolItemSuitById(String id) {
        QueryWrapper<CabalToolItemScript> queryWrapper = new QueryWrapper<CabalToolItemScript>();
        queryWrapper.lambda().eq(CabalToolItemScript::getSuitId, Long.valueOf(id));
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        cabalToolItemScriptMapper.delete(queryWrapper);
        cabalToolItemSuitMapper.deleteById(Long.parseLong(id));
        DynamicDataSourceContextHolder.removeContextKey();
        return "SUCCESS";
    }

    @Override
    public String deleteCabalToolItemScriptById(String id) {
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        cabalToolItemScriptMapper.deleteById(Long.parseLong(id));
        DynamicDataSourceContextHolder.removeContextKey();
        return "SUCCESS";
    }

    @Override
    public String sendEquipment(CabalToolItemScriptDTO cabalToolItemScriptDTO) {
        Long tranNo = System.currentTimeMillis();

        // 物品发送
        sendByItemScript(cabalToolItemScriptDTO, tranNo);
        return tranNo.toString();
    }

    private void sendByItemScript(CabalToolItemScriptDTO cabalToolItemScriptDTO, Long tranNo) {
        Integer amount = cabalToolItemScriptDTO.getAmount();
        // 物品发送
        Map<String, Object> sendItemParams = new HashMap<>();
        sendItemParams.put("itemIdx", cabalToolItemScriptDTO.getItemKindIdx());
        sendItemParams.put("itemOpt", cabalToolItemScriptDTO.getItemOpt());
        sendItemParams.put("durationIdx", cabalToolItemScriptDTO.getDurationIdx());
        sendItemParams.put("account", cabalToolItemScriptDTO.getAccount());
        sendItemParams.put("tranNo", tranNo);
        DynamicDataSourceContextHolder.removeContextKey();
        if(amount > 1) {
            for(int i=0;i<amount;i++) {
                cabalAuthMapper.execSendItemByTools(sendItemParams);
            }
        } else {
            cabalAuthMapper.execSendItemByTools(sendItemParams);
        }
    }

    private List<CabalToolItemScript> selectCabalToolItemScriptListBySuitId(long suitId) {
        QueryWrapper<CabalToolItemScript> queryWrapper = new QueryWrapper<CabalToolItemScript>();
        queryWrapper.lambda().eq(CabalToolItemScript::getSuitId, suitId);
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        List<CabalToolItemScript> cabalToolItemScriptList =  cabalToolItemScriptMapper.selectList(queryWrapper);
        DynamicDataSourceContextHolder.removeContextKey();
        return cabalToolItemScriptList;
    }

    @Override
    public String sendItemSuit(CabalToolItemSuitDTO cabalToolItemSuitDTO) {
        List<CabalToolItemScript> cabalToolItemScriptList = this.selectCabalToolItemScriptListBySuitId(Long.parseLong(cabalToolItemSuitDTO.getId()));
        Long tranNo = System.currentTimeMillis();
        for(CabalToolItemScript cabalToolItemScript : cabalToolItemScriptList) {
            CabalToolItemScriptDTO cabalToolItemScriptDTO = new CabalToolItemScriptDTO();
            BeanUtils.copyProperties(cabalToolItemScript, cabalToolItemScriptDTO);
            cabalToolItemScriptDTO.setAccount(cabalToolItemSuitDTO.getAccount());
            sendByItemScript(cabalToolItemScriptDTO, tranNo);
        }
        return tranNo.toString();
    }

    @Override
    public String revokeSend(String tranNo) {
        cabalAuthMapper.revokeSend(Long.parseLong(tranNo));
        return "SUCCESS";
    }

    @Override
    public String copySuit(CabalToolItemSuitDTO cabalToolItemSuitDTO) {
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        CabalToolItemSuit dbCabalToolItemSuit = cabalToolItemSuitMapper.selectById(Long.parseLong(cabalToolItemSuitDTO.getId()));
        if(dbCabalToolItemSuit == null) {
            return "SUCCESS";
        }

        CabalToolItemSuit newCabalToolItemSuit = new CabalToolItemSuit();
        BeanUtils.copyProperties(dbCabalToolItemSuit, newCabalToolItemSuit);
        newCabalToolItemSuit.setId(SnowflakeIdUtil.getInstance().nextId());
        newCabalToolItemSuit.setItemSuitName(dbCabalToolItemSuit.getItemSuitName().concat("("+System.currentTimeMillis()+")"));
        cabalToolItemSuitMapper.insert(newCabalToolItemSuit);

        QueryWrapper<CabalToolItemScript> queryWrapperScript = new QueryWrapper<CabalToolItemScript>();
        queryWrapperScript.lambda().eq(CabalToolItemScript::getSuitId, dbCabalToolItemSuit.getId());
        List<CabalToolItemScript> cabalToolItemScriptList =  cabalToolItemScriptMapper.selectList(queryWrapperScript);
        for(CabalToolItemScript cabalToolItemScript : cabalToolItemScriptList) {
            CabalToolItemScript newCabalToolItemScript = new CabalToolItemScript();
            BeanUtils.copyProperties(cabalToolItemScript, newCabalToolItemScript);
            newCabalToolItemScript.setId(SnowflakeIdUtil.getInstance().nextId());
            newCabalToolItemScript.setSuitId(newCabalToolItemSuit.getId());
            cabalToolItemScriptMapper.insert(newCabalToolItemScript);
        }
        DynamicDataSourceContextHolder.removeContextKey();
        return "SUCCESS";
    }
}
